package com.luoye.lj.wstool.web;

import com.luoye.lj.wstool.web.entity.WebtEntity;
import com.luoye.lj.wstool.web.entity.WebtField;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/*
 * Created with IntelliJ IDEA.
 * User: Dave Liu
 * Date: 2016/4/26
 * Time: 13:06
 * To change this template use File | Settings | File Templates.
 */
public class WebtResolver {
    public static WebtEntity resolve(String webtFullPath, String webtStr){
        WebtEntity webtEntity=null;
        File file=new File(webtFullPath);
        if(file.exists()){
            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
                String line=null;
                webtEntity=new WebtEntity();
                String entityName=webtFullPath.substring(webtFullPath.lastIndexOf("/")+1,webtFullPath.lastIndexOf(".webt"));
                webtEntity.setEntityName(entityName);
                String entityPackageName=webtStr.substring(0,webtStr.lastIndexOf("."));
                webtEntity.setControllerPackage(entityPackageName);
                List<WebtField> webtFieldList=new ArrayList<WebtField>();
                webtEntity.setFieldList(webtFieldList);
                while ((line=reader.readLine())!=null){
                    line=line.trim();
                    if("".equals(line)||line.startsWith("#")){
                        continue;
                    }
                    if(line.startsWith("domain:")){//实体类
                        line=line.substring("domain:".length());
                        String[] words=line.split("-");
                        if(words.length>=8){
                            webtEntity.setDescription(words[7]);
                        }
                        if(words.length>=7){
                            webtEntity.setWebPath(words[6]);
                        }
                        if(words.length>=6){
                            webtEntity.setControllerPackage(words[5]);
                        }
                        if(words.length>=5){
                            if(words[4].startsWith("/")) {
                                webtEntity.setJspDir(words[4]);
                            }else {
                                webtEntity.setJspDir("/"+words[4]);
                            }
                        }
                        if(words.length>=4){
                            if(words[3].startsWith("/")) {
                                webtEntity.setJspPrefix(words[3]);
                            }else {
                                webtEntity.setJspPrefix("/"+words[3]);
                            }
                        }
                        if(words.length>=3){
                            webtEntity.setPath(words[2]);
                        }
                        if(words.length>=2){
                            webtEntity.setUrlProperty(words[1]);
                        }
                        webtEntity.setEntityName(words[0]);
                    }else{//字段信息
                        String[] words=line.split("-");
                        if(words.length>=4){
                            WebtField webtField=new WebtField(words[0],words[1],words[2],words[3]);
                            if(words.length>=5){
                                webtField.setPrimary(true);
                            }
                            if(words.length>=6){
                                webtField.setIncrement(true);
                            }
                            webtFieldList.add(webtField);
                        }
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (reader != null) {
                        reader.close();
                        System.out.println("finally resolve");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return webtEntity;
    }
}
